package com.ids.idtma;

import java.io.File;
import java.io.InputStream;
import java.io.ObjectInputStream.GetField;

import org.apache.http.util.EncodingUtils;
import org.greenrobot.eventbus.EventBus;

import com.ids.idtma.map.IdtMap;
import com.ids.idtma.map.IdtMapOffline;
import com.ids.idtma.provider.SmsConversationProvider;
import com.ids.idtma.provider.SmsProvider;
import com.ids.idtma.service.EventMessage;
import com.ids.idtma.setting.DebugSettingActivity;
import com.ids.idtma.setting.MeiTiSettingActivity;
import com.ids.idtma.setting.ShiPinSettingActivity;
import com.ids.idtma.setting.TongDaoSettingActivity;
import com.ids.idtma.setting.YuYinSettingActivity;
import com.ids.idtma.util.CacheCleanUtils;
import com.ids.idtma.util.CustomDialog;
import com.ids.idtma.util.LwtLog;
import com.ids.proxy.IDSApiProxyMgr;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SystemSetting extends ActivityBase implements OnClickListener {

	private TextView page_title_name;
	private TextView huancun_size;
	private String cacheSize;
	private String[] splits;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_ui_idt_system_setting);
		IdtApplication.getInstance().addActivity(this);
		initData();
		initView();
		
	}

	private void initData() {
		String resut = "";
		try {
			InputStream is = getResources().openRawResource(R.raw.idt);
			int len = is.available();
			byte []buffer = new byte[len];
			is.read(buffer);
			resut = EncodingUtils.getString(buffer, "gbk");
			is.close();
			} catch (Exception e) {
			e.printStackTrace();
			}		
		splits = resut.split("\n");
		//String string = splits[0];
		
	}

	private void initView() {
		RelativeLayout yuyin_chuli = (RelativeLayout) findViewById(R.id.yuyin_chuli);
		RelativeLayout meiti_chuanshu = (RelativeLayout) findViewById(R.id.meiti_chuanshu);
		RelativeLayout yuyin_peizhi = (RelativeLayout) findViewById(R.id.yuyin_peizhi);
		RelativeLayout shipin_peizhi = (RelativeLayout) findViewById(R.id.shipin_peizhi);
		RelativeLayout tongdao_shijian = (RelativeLayout) findViewById(R.id.tongdao_shijian);
		RelativeLayout qingli_huancun = (RelativeLayout) findViewById(R.id.qingli_huancun);
		RelativeLayout shanchu_all = (RelativeLayout) findViewById(R.id.shanchu_all);
		RelativeLayout tiaoshi_xinxi = (RelativeLayout) findViewById(R.id.tiaoshi_xinxi);
		huancun_size = (TextView) findViewById(R.id.huancun_size);
		page_title_name = (TextView) findViewById(R.id.page_title_name);
		page_title_name.setText("高级选项");
		yuyin_chuli.setOnClickListener(this);
		meiti_chuanshu.setOnClickListener(this);
		yuyin_peizhi.setOnClickListener(this);
		shipin_peizhi.setOnClickListener(this);
		tongdao_shijian.setOnClickListener(this);
		qingli_huancun.setOnClickListener(this);
		shanchu_all.setOnClickListener(this);
		tiaoshi_xinxi.setOnClickListener(this);
		try {
			cacheSize = CacheCleanUtils.getCacheSize(new File(
					"/mnt/sdcard/IDT-MA"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		huancun_size.setText(cacheSize);
	}

	@SuppressLint("SdCardPath")
	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.return_button:// 返回按钮
			IdtApplication.getInstance().deleteActivity(this);
			break;
		case R.id.map:
			// 点击地图图标
			intent.setClass(SystemSetting.this, IdtMap.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;

		case R.id.yuyin_chuli://语音处理
			//String idt_ReadIni = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "SYSTEM", "MEDPORT");
			//int idt_WriteIni = IDSApiProxyMgr.getCurProxy().IDT_WriteIni("/data/data/com.ids.idtma/files/IDT.ini", "SYSTEM", "MEDPORT", "20000");
			//LwtLog.d("xiezhixian", "jni方法读出啦的内容"+idt_ReadIni);
			//LwtLog.d("xiezhixian", "jni方法写的结果"+idt_WriteIni);
			break;
		case R.id.meiti_chuanshu://媒体配置
//			for (int i = 0; i < splits.length; i++) {
//				String idtResut = splits[i];
//				if("[MEDIA_TRANS]".equals(idtResut)){
//					String prot0 = splits[i+1];
//					String prot1 = splits[i+2];
//					String rtp_audio = splits[i+3];
//					String rtp_video = splits[i+4];
//					String tcp_audio = splits[i+5];
//					String tcp_video = splits[i+6];
			
					String prot0 = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "MEDIA_TRANS", "PROT_0");
					String prot1 = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "MEDIA_TRANS", "PROT_1");
					String rtp_audio = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "MEDIA_TRANS", "RTP_AUDIO");
					String rtp_video = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "MEDIA_TRANS", "RTP_VIDEO");
					String tcp_audio = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "MEDIA_TRANS", "TCP_AUDIO");
					String tcp_video = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "MEDIA_TRANS", "TCP_VIDEO");

					intent.setClass(SystemSetting.this, MeiTiSettingActivity.class);
					intent.putExtra("PROT_0", prot0);
					intent.putExtra("PROT_1", prot1);
					intent.putExtra("RTP_AUDIO", rtp_audio);
					intent.putExtra("RTP_VIDEO", rtp_video);
					intent.putExtra("TCP_AUDIO", tcp_audio);
					intent.putExtra("TCP_VIDEO", tcp_video);
					startActivity(intent);
			break;
		case R.id.yuyin_peizhi://语音配置
//			for (int i = 0; i < splits.length; i++) {
//				String idtResut = splits[i];
//				if("[RTP_AUDIO_0]".equals(idtResut)){
//					String code = splits[i+1];
//					String pkgtime = splits[i+2];
//					intent.setClass(SystemSetting.this, YuYinSettingActivity.class);
//					intent.putExtra("AUDIO_CODE", code);
//					intent.putExtra("AUDIO_PKGTIME", pkgtime);
//					startActivity(intent);
//				}
//			}
			String code = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "RTP_AUDIO_0", "CODE");
			String pkgtime = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "RTP_AUDIO_0", "PKGTIME");
			intent.setClass(SystemSetting.this, YuYinSettingActivity.class);
			intent.putExtra("AUDIO_CODE", code);
			intent.putExtra("AUDIO_PKGTIME", pkgtime);
			startActivity(intent);
			break;
		case R.id.shipin_peizhi://视频配置[RTP_VIDEO_0]
//			for (int i = 0; i < splits.length; i++) {
//				String idtResut = splits[i];
//				if("[RTP_VIDEO_0]".equals(idtResut)){
//					String code = splits[i+1];
//					String width = splits[i+2];
//					String height = splits[i+3];
//					String bitrate = splits[i+4];
//					String fps = splits[i+5];
//					String gop = splits[i+6];
//					intent.setClass(SystemSetting.this, ShiPinSettingActivity.class);
//					intent.putExtra("VIDEO_CODE", code);
//					intent.putExtra("VIDEO_WIDTH", width);
//					intent.putExtra("VIDEO_HEIGHT", height);
//					intent.putExtra("VIDEO_BITRATE", bitrate);
//					intent.putExtra("VIDEO_FPS", fps);
//					intent.putExtra("VIDEO_GOP", gop);
//					startActivity(intent);
//				}
//			}
			String code_video = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "RTP_VIDEO_0", "CODE");
			String width = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "RTP_VIDEO_0", "WIDTH");
			String height = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "RTP_VIDEO_0", "HEIGHT");
			String bitrate = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "RTP_VIDEO_0", "BITRATE");
			String fps = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "RTP_VIDEO_0", "FPS");
			String gop = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "RTP_VIDEO_0", "GOP");
			intent.setClass(SystemSetting.this, ShiPinSettingActivity.class);
			intent.putExtra("VIDEO_CODE", code_video);
			intent.putExtra("VIDEO_WIDTH", width);
			intent.putExtra("VIDEO_HEIGHT", height);
			intent.putExtra("VIDEO_BITRATE", bitrate);
			intent.putExtra("VIDEO_FPS", fps);
			intent.putExtra("VIDEO_GOP", gop);
			startActivity(intent);
			
			break;
		case R.id.tongdao_shijian://通道保活时间
//			for (int i = 0; i < splits.length; i++) {
//				String idtResut = splits[i];
//				if("[SYSTEM]".equals(idtResut)){
//					String sigport = splits[i+1];
//					String medport = splits[i+2];
//					String subs_num = splits[i+4];
//					String subs_level = splits[i+6];
//					intent.setClass(SystemSetting.this, TongDaoSettingActivity.class);
//					intent.putExtra("SIGPORT", sigport);
//					intent.putExtra("MEDPORT", medport);
//					intent.putExtra("SUBS_NUM", subs_num);
//					intent.putExtra("SUBS_LEVEL", subs_level);
//					startActivity(intent);
//				}
//			}
					String sigport = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "SYSTEM", "SIGPORT");
					String medport = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "SYSTEM", "MEDPORT");
					String subs_num = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "SYSTEM", "SUBS_NUM");
					String subs_level = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "SYSTEM", "SUBS_LEVEL");
					intent.setClass(SystemSetting.this, TongDaoSettingActivity.class);
					intent.putExtra("SIGPORT", sigport);
					intent.putExtra("MEDPORT", medport);
					intent.putExtra("SUBS_NUM", subs_num);
					intent.putExtra("SUBS_LEVEL", subs_level);
					startActivity(intent);
			break;
		case R.id.tiaoshi_xinxi:
			String port = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "DEBUG", "PORT");
			String screen = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "DEBUG", "SCREEN");
			String level = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "DEBUG", "LEVEL");
			String log = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "DEBUG", "LOG");
			String saveframe = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "DEBUG", "SAVEFRAME");
			String transdebug = IDSApiProxyMgr.getCurProxy().IDT_ReadIni("/data/data/com.ids.idtma/files/IDT.ini", "DEBUG", "TRANSDEBUG");
			
			intent.setClass(SystemSetting.this, DebugSettingActivity.class);
			intent.putExtra("PORT", port);
			intent.putExtra("SCREEN", screen);
			intent.putExtra("LEVEL", level);
			intent.putExtra("LOG", log);
			intent.putExtra("SAVEFRAME", saveframe);
			intent.putExtra("TRANSDEBUG", transdebug);
			startActivity(intent);
			break;
		case R.id.qingli_huancun:
			new CustomDialog.Builder(this)
					.setTitle("温馨提示")
					.setMessage("删除的文件不可恢复,您确定要全部删除吗?")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									switch (which) {
									case AlertDialog.BUTTON_POSITIVE:
										try {
											CacheCleanUtils.deleteFolderFile((new File("/mnt/sdcard/IDT-MA")).getPath(), true);
											String cacheSize = CacheCleanUtils.getCacheSize(new File("/mnt/sdcard/IDT-MA"));
											huancun_size.setText(cacheSize);
										} catch (Exception e) {
											e.printStackTrace();
										}
										if (dialog != null) {
											dialog.dismiss();
										}
										break;
									case AlertDialog.BUTTON_NEGATIVE:
										if (dialog != null) {
											dialog.dismiss();
										}
										break;
									default:
										break;
									}
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									if (dialog != null) {
										dialog.dismiss();
									}
								}
							}).create().show();
			break;
		case R.id.shanchu_all:
			  final Builder builder = new AlertDialog.Builder(SystemSetting.this);
			   new AlertDialog.Builder(SystemSetting.this).setTitle("温馨提示")
			     .setMessage("删除的记录不可恢复,您确定要全部删除吗?") 
			     .setPositiveButton("确定",new DialogInterface.OnClickListener() {
			         @Override  
			         public void onClick(DialogInterface dialog, int which) {
			        	 Uri uri = SmsProvider.CONTENT_URI;
			 			ContentResolver resolver = getContentResolver();
			 			resolver.delete(uri, null, null);
			 			Toast.makeText(SystemSetting.this, "所有聊天内容已清除", Toast.LENGTH_SHORT).show();
			 			EventBus.getDefault().post(new EventMessage("deleteAll", "deleteAll"));
			 			builder.create().dismiss();
			         }  
			  
			     }).setNegativeButton("返回",new DialogInterface.OnClickListener() {
			         @Override  
			  
			         public void onClick(DialogInterface dialog, int which) {
			        	 builder.create().dismiss();
			         }  
			  
			     }).show();//在按键响应事件中显示此对话框  
			break;
		default:
			break;
		}
	}
}
